<?php
/**
 * Created by Boume.
 * Date: 5/14 0014
 * Time: 9:29
 */

namespace app\admin\controller;

use cos\Sts;

class Fun extends Base
{
    //上传图片
    public function up_img()
    {
        $file = request()->file('file');
        $info = $file->move('./uploads');
        if ($info) {
            return json([
                'code' => 1,
                'msg' => '上传成功',
                'src' => '/uploads/' . str_replace('\\', '/', $info->getSaveName()),
                'name' => explode('.', $_FILES['file']['name'])[0]
            ]);
        } else {
            $this->error($file->getError());
        }
    }

    public function ckeditor_upimg()
    {
        $file = request()->file('upload');
        $info = $file->move('./uploads');
        if ($info) {
            return json([
                'uploaded' => 1,
                'fileName' => $_FILES['upload']['name'],
                'url' => '/uploads/' . str_replace('\\', '/', $info->getSaveName()),
            ]);
        } else {
            return json([
                'uploaded' => 0,
                'error' => $file->getError(),
            ]);
        }
    }

    public function CosSts()
    {
        $sts = new Sts();
        $config = array(
            'url' => 'https://sts.tencentcloudapi.com/',
            'domain' => 'sts.tencentcloudapi.com',
            'proxy' => '',
            'secretId' => config('storage.cos.secretId'), // 固定密钥
            'secretKey' => config('storage.cos.secretKey'), // 固定密钥
            'bucket' => config('storage.cos.bucket'), // 换成你的 bucket
            'region' => config('storage.cos.region'), // 换成 bucket 所在园区
            'durationSeconds' => 1800, // 密钥有效期
            'allowPrefix' => '*', // 这里改成允许的路径前缀，可以根据自己网站的用户登录态判断允许上传的目录，例子：* 或者 a/* 或者 a.jpg
            'allowActions' => [
                // 简单上传
                'name/cos:PutObject',
                'name/cos:PostObject',
                // 分片上传
                'name/cos:InitiateMultipartUpload',
                'name/cos:ListMultipartUploads',
                'name/cos:ListParts',
                'name/cos:UploadPart',
                'name/cos:CompleteMultipartUpload'
            ],
        );
        // 获取临时密钥，计算签名
        $tempKeys = $sts->getTempKeys($config);
        return json($tempKeys);
    }

}